<?php

class payment extends Controller {

	function payment()
	{
		parent::Controller();	
	}
	
	function index()
	{
		$this->load->model('TutorLandAccountModel');
		$data['account'] = $this->TutorLandAccountModel->getAll();
		
		$this->load->model('filemanagementmodel');
	    $data['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $data['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		
		$this->load->view('f_header', $data);
		$this->load->view('f_payment', $data);
		$this->load->view('f_footer', $data);
	}
	
	function save() 
	{
		$data['first_name'] = $this->input->post('txtFirstName') ? $this->input->post('txtFirstName') : null;		
		$data['last_name'] = $this->input->post('txtLastName') ? $this->input->post('txtLastName') : null;
		$data['status'] = $this->input->post('txtStatus') ? $this->input->post('txtStatus') : null;
		$data['mobile_no'] = $this->input->post('txtTel') ? $this->input->post('txtTel') : null;
		$data['email'] = $this->input->post('txtEmail') ? $this->input->post('txtEmail') : null;
		$data['tranfer_method'] = $this->input->post('txtTransfer') ? $this->input->post('txtTransfer') : null;
		$data['transfer_method_0ther'] = $this->input->post('txtTransferOther') ? $this->input->post('txtTransferOther') : null;
		$data['tranfer_to_account_id'] = $this->input->post('radAccount') ? $this->input->post('radAccount') : null;		
		$data['amount'] = $this->input->post('txtAmount') ? $this->input->post('txtAmount') : null;
		$data['pay'] = $this->input->post('txtPayFor') ? $this->input->post('txtPayFor') : null;
		$data['pay_detail'] = $this->input->post('txtPayForOther') ? $this->input->post('txtPayForOther') : null;
		$data['transfer_date'] = $this->input->post('txtDate') ? $this->input->post('txtDate') : null;
		$data['transfer_hr'] = $this->input->post('txtHr') ? $this->input->post('txtHr') : null;
		$data['transfer_min'] = $this->input->post('txtMin') ? $this->input->post('txtMin') : null;		
		$data['note'] = $this->input->post('txtNote') ? $this->input->post('txtNote') : null;
		
		$this->load->model('paymentmodel');
		$id = $this->paymentmodel->add($data);
		$msg['success'] = "ได้รับข้อความเรียบร้อยแล้วค่ะ ทางโรงเรียนจะติดต่อกลับไปเร็วที่สุดค่ะ";
		
		if(isset($id) && isset($_FILES) && @$_FILES['fileUpload']['error'] == '0') {
			// save file
			$config['upload_path'] = './data-image/transfer/';
			$config['allowed_types'] = 'gif|jpg|png|pdf|zip|doc|tif';
			$config['max_size']	= '2048';
			$config['overwrite'] = TRUE;
			$config['file_name'] = $id.'-transfer';
			$this->load->library('upload', $config);
			if ( ! $this->upload->do_upload('fileUpload'))
			{
				$msg['success'] = "ไม่สามารถ upload ไฟล์แนบได้";			
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$Imgdata = array(
				   'payment_id' => $id, 
	               'transfer_copy' => $data['upload_data']['file_name']   
	            );
				$this->paymentmodel->update($Imgdata, null);
				$msg['success'] = "ได้รับข้อความเรียบร้อยแล้วค่ะ ทางโรงเรียนจะติดต่อกลับไปเร็วที่สุดค่ะ";
			}
		}
		
		// send mail;
		try {
				$this->load->library('email');
				$config['charset'] = 'utf-8';
				$config['wordwrap'] = TRUE;
				$config['mailtype'] = 'html';
				$this->email->initialize($config);
				
				$this->email->from($data['email'], $data['first_name']);
				$this->email->to('connect_bvk@hotmail.com');
//				$this->email->cc('connect_bvk@hotmail.com');
				
				$this->email->subject('[C] มีรายการชำระเงิน จากคุณ ' . $data['first_name']);
				$this->load->model('TutorLandAccountModel');
				$account_no = $this->TutorLandAccountModel->getByID($data['tranfer_to_account_id']);
		
				$msgMail = '
				<html>
				<head>
				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
				</head>
				
				<body>
				<p>มีรายการชำระเงิน จากคุณ' . $data['first_name'].' '.$data['last_name']. '</p>
				<table width="100%" border="0" cellspacing="0" cellpadding="0">
				  <tr>
				    <td>สถานะ</td>
				    <td>'. $data['status'] .'</td>
				  </tr>
				  <tr>
				    <td>โทรศัพท์</td>
				    <td>'. $data['mobile_no'] .'</td>
				  </tr>
				  <tr>
				    <td>วิธีการชำระเงิน</td>
				    <td>'. $data['tranfer_method'].' '. $data['transfer_method_0ther'] .'</td>
				  </tr>
				  <tr>
				    <td>เข้าบัญชี</td>
				    <td>'.$account_no['bank_name'] . ' '.$account_no['account_no'] .'</td>
				  </tr>
				  <tr>
				    <td>จำนวนเงิน</td>
				    <td>'. $data['amount'] .' บาท</td>
				  </tr>
				  <tr>
				    <td>ชำระค่า</td>
				    <td>'. $data['pay'].' '. $data['pay_detail'] .'</td>
				  </tr>
				  <tr>
				    <td>วันที่ชำระเงิน</td>
				    <td>'. $data['transfer_date'].' '. $data['transfer_hr'] .':'. $data['transfer_min']  .' น.</td>
				  </tr>
				</table>
				<br />
				<hr />
				<div align="right"><em>ข้อความนี้ถูกส่งโดยระบบหลังบ้านของติวเตอร์แลนด์<br /> 
				' . date('jS F Y h:i:s A') .'</em></div>
				</body></html>';
				$this->email->message($msgMail);
				
				$this->email->send();
		} catch (Exception $e) {
		}
		$this->load->model('TutorLandAccountModel');
		$msg['account'] = $this->TutorLandAccountModel->getAll();

		$this->load->model('filemanagementmodel');
	    $msg['header_tel'] = $this->filemanagementmodel->getByPostion('header_tel');
	    $msg['footer_address'] = $this->filemanagementmodel->getByPostion('footer_add1');
		
		$this->load->view('f_header', $msg);
		$this->load->view('f_payment', $msg);
		$this->load->view('f_footer', $msg);
	}
	
	function lists()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->library('pagination');
 		$this->load->model('paymentmodel');
	    $per_page = 15;
   		$total = $this->paymentmodel->countAll();
   		$data['trans'] = $this->paymentmodel->getAllLimit($per_page, $this->uri->segment(3));
	    
   		$base_url = site_url('payment/lists/');
	    $config['base_url'] = $base_url;
	    $config['total_rows'] = $total;
	    $config['per_page'] = $per_page;
	    $config['uri_segment'] = '3';
	
	    $this->pagination->initialize($config); 
		$data['paging'] = str_replace('lists.html/', 'lists/', $this->pagination->create_links());
	   
	    
	    $this->load->view('header');
		$this->load->view('payment-list-form', $data);
		$this->load->view('footer');
	    
	}
	
	function approve()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('paymentmodel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$data['payment_id'] = $id[$i];
	    	$data['backend_stauts'] = 'confirm';
	    	$this->paymentmodel->update($data,'admin');
	    }	    
	    $this->lists();
	}
	
	function detail($id)
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('paymentmodel');
	    $data = $this->paymentmodel->getByID($id);
	    $this->load->model('TutorLandAccountModel');
		$data['account'] = $this->TutorLandAccountModel->getByID($data['tranfer_to_account_id']);
	    $this->load->view('header');
		$this->load->view('payment-detail-form', $data);
		$this->load->view('footer');
	    
	}
	
	function delete()
	{
		if ($this->session->userdata('logged_in') != TRUE)
	    {
	        redirect('login/index');
	    }
	    
	    $this->load->model('paymentmodel');
	    $id = $this->input->post('txtDel');
	    for($i = 0; $i < count($id); $i++) {
	    	$data['payment_id'] = $id[$i];
	    	$data['backend_stauts'] = 'cancel';
	    	$data['is_active'] = '0';
	    	$this->paymentmodel->update($data, null);
	    }	    
	    $this->lists();
	}
}

?>